﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


namespace VideoWorld
{
    /// <summary>
    /// 取回密码页面.
    /// </summary>
    public class GetPassword : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Image Image1;
        protected System.Web.UI.WebControls.Label ErrorInfo;
        protected System.Web.UI.WebControls.Button btGetPassword;
        protected System.Web.UI.WebControls.Button btReturnLogin;
        protected System.Web.UI.WebControls.TextBox tbName;
        protected System.Web.UI.WebControls.TextBox tbCode;

        DataAccess.Users users = new DataAccess.Users(Global.ConnectionString);

        private void Page_Load(object sender, System.EventArgs e)
        {
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btGetPassword.Click += new System.EventHandler(this.btGetPassword_Click);
            this.btReturnLogin.Click += new System.EventHandler(this.btReturnLogin_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void btGetPassword_Click(object sender, System.EventArgs e)
        {
            Check();
            SendPassword();
        }

        private void btReturnLogin_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("login.aspx");
        }

        //检查用户输入
        private void Check()
        {
            String code = Session["VerifyCode"] as String;

            if (Business.Users.VerifyText(tbName.Text))
            {
                ErrorInfo.Text = String.Format("提示：请输入帐号!");
                return;
            }

            if (!Business.Users.IsValidEmail(tbName.Text))
            {
                ErrorInfo.Text = String.Format("提示：帐号是E-mail格式!");
                return;
            }

            if (String.Compare(tbCode.Text, code, true) != 0)
            {
                ErrorInfo.Text = String.Format("提示：验证码输入有误!");
                return;
            }

            if (users.IsUserExist(tbName.Text))
            {
                ErrorInfo.Text = String.Format("提示：该用户未注册!");
                return;
            }
        }
        //通过邮件方式把密码发给客户
        private void SendPassword()
        {
            try
            {
                //发密码邮件给用户
                String password = users.GetUserPassword(tbName.Text);
                string mailFrom = "VideoWorld";
                string mailTo = tbName.Text;
                string mailSubject = "来自VideoWorld的密码信";
                string mailBody = String.Format("尊敬的用户您好，您在VideoWorld的用户为：{0}，密码为：{1}", tbName.Text, password);
                string serverName = "Mail.VideoWorld.com";
                string mailServerUserName = "Admin";
                string mailServerPassword = "123456";

                Common.Mail.SendMail(mailFrom, mailTo, mailSubject, mailBody, serverName, mailServerUserName, mailServerPassword);

                ErrorInfo.Text = String.Format("提示：系统已经将你注册时侯的密码发到了你的邮箱中，请查收!");
                btGetPassword.Enabled = false;
            }
            catch
            {
                ErrorInfo.Text = String.Format("提示：系统繁忙请稍后再试!");
            }
        }

    }
}
